package com.luoxiang.socialandroid.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.activity.CommunityViedioActivity;
import com.luoxiang.socialandroid.activity.GetimageActivity;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.LogUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @Description TODO
 * @systemUser Administrator
 * @Author LX
 * @Date 08-30-2022 周二 15:26
 */
public class CommunityimgRvAdapter extends RecyclerView.Adapter<CommunityimgRvAdapter.ViewHolder> {
    private List<String> list;
    private String icon;
    private String name;
    private String title;
    private int mType;
    private View view;
    private int userid;
    List<String> list1;
    private String mDomain;
    private int isFoucles, dymainid;

    public CommunityimgRvAdapter() {


    }

    public void setData(int dymainid, List<String> list1, int userId, int isFoucles, String icon, String name, String title, List<String> list, int type, String domain) {
        this.dymainid = dymainid;
        this.list1 = list1;
        userid = userId;
        this.isFoucles = isFoucles;
        this.icon = icon;
        this.name = name;
        this.title = title;
        this.list = list;
        this.mType = type;
        mDomain = domain;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_communityimg, parent, false);
        CommunityimgRvAdapter.ViewHolder holder = new CommunityimgRvAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (list.size() == 1) {
            //展示图片
            String imgUrl = mDomain + list.get(position);
            LogUtils.i("domain", mDomain);
            Glide.with(MyAppliaction.getContext())
                    .load(imgUrl)
                    .transform(new CenterCrop(),new RoundedCorners(18))
                    .placeholder(R.mipmap.icon_error_img2)
                    .error(R.mipmap.icon_error_img2)
                    .into(holder.ic_rvimgone);
//            ImageView imageView = holder.ic_rvimgone;
//            imageView.setScaleType(ImageView.ScaleType.CENTER);
            holder.ic_rvimgone.setVisibility(View.VISIBLE);
            holder.ic_rvimg.setVisibility(View.GONE);
            holder.ic_rvimgtwo.setVisibility(View.GONE);
        } else if (list.size() == 2) {
            String imgUrl = mDomain + list.get(position);
            LogUtils.i("domain", mDomain);
            Glide.with(MyAppliaction.getContext())
                    .load(imgUrl)
                    .transform(new CenterCrop(),new RoundedCorners(18))
                    .placeholder(R.mipmap.icon_error_img2)
                    .error(R.mipmap.icon_error_img2)
                    .into(holder.ic_rvimgtwo);
//            ImageView imageView =  holder.ic_rvimgtwo;
//            imageView.setScaleType(ImageView.ScaleType.CENTER);
            holder.ic_rvimgtwo.setVisibility(View.VISIBLE);
            holder.ic_rvimg.setVisibility(View.GONE);
            holder.ic_rvimgone.setVisibility(View.GONE);
        } else {
            String imgUrl = mDomain + list.get(position);
            LogUtils.i("domain", mDomain);
            Glide.with(MyAppliaction.getContext())
                    .load(imgUrl)
                    .transform(new CenterCrop(),new RoundedCorners(18))
                    .placeholder(R.mipmap.icon_error_img2)
                    .error(R.mipmap.icon_error_img2)
                    .into(holder.ic_rvimg);
//            ImageView imageView = holder.ic_rvimg;
//            imageView.setScaleType(ImageView.ScaleType.CENTER);
            holder.ic_rvimgone.setVisibility(View.GONE);
            holder.ic_rvimg.setVisibility(View.VISIBLE);
            holder.ic_rvimgtwo.setVisibility(View.GONE);
        }

        if (mType == 0) {
            holder.img_isshipin.setVisibility(View.GONE);
        } else {
            holder.img_isshipin.setVisibility(View.VISIBLE);
        }

        holder.itemView.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                if (mType == 0) {
                    //查看图片

                    Intent intent = new Intent(view.getContext(), GetimageActivity.class);
                    intent.putExtra("icon", icon);
                    intent.putExtra("name", name);
                    intent.putExtra("title", title);
                    intent.putExtra("domain", mDomain);
                    intent.putExtra("bannerList", (Serializable) list);
                    intent.putExtra("isFoucles", isFoucles);
                    intent.putExtra("userId", userid);
                    intent.putExtra("mposition", position);
                    intent.putExtra("dymainId", dymainid);
                    view.getContext().startActivity(intent);
                } else {
                    //播放视频
                    Intent intent = new Intent(view.getContext(), CommunityViedioActivity.class);
                    intent.putExtra("icons", icon);
                    intent.putExtra("names", name);
                    intent.putExtra("titles", title);
                    intent.putExtra("domains", mDomain);
                    intent.putExtra("bannerLists", list1.get(0));
                    intent.putExtra("isFoucless", isFoucles);
                    intent.putExtra("userIds", userid);
                    intent.putExtra("mpositions", position);
                    intent.putExtra("dymainId", dymainid);
                    view.getContext().startActivity(intent);
                }
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ic_rvimg;
        ImageView img_isshipin, ic_rvimgone, ic_rvimgtwo;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ic_rvimg = itemView.findViewById(R.id.ic_rvimg);
            img_isshipin = itemView.findViewById(R.id.img_isshipin);
            ic_rvimgone = itemView.findViewById(R.id.ic_rvimgone);
            ic_rvimgtwo = itemView.findViewById(R.id.ic_rvimgtwo);

        }
    }
}
